<template>
  <div id="search-header">
    <!-- 返回、 搜索 -->
    <div class="top">
      <svg class="icon icon-left" aria-hidden="true" @click="$router.go(-1)">
        <use xlink:href="#icon-xitongfanhui" />
      </svg>
      <input type="text" v-model="searchKeyword" :placeholder="placeholder" @keydown.enter="saveKeyword" />
    </div>

    <!-- 历史记录 -->
    <div class="history">
      <div class="historyLeft">搜索历史</div>
      <div class="historyRight">
        <div class="kyewordItem" v-for="(item,i) in keywordList" :key="i" @click="historySearch(item)">{{item}}</div>
      </div>
    </div>

    <!-- 搜索结果列表 -->
    <div class="music-list">
      <div class="list-item" v-for="(item,i) in searchMusic" :key="i" @click="play(item,i)">
        <span class="index">{{i}}</span>
        <div class="music-name">{{item.name}}</div>
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-boshiweb_bofangliang" />
        </svg>
      </div>
    </div>
  </div>
</template>

<script>
import { searchMusic } from '@/Api/request.js'

export default {
  name: 'SearchHeader',
  data() {
    return {
      placeholder: '陈奕迅',
      keywordList: [],
      searchKeyword: '',
      searchMusic: []
    }
  },
  beforeMount() {
    this.keywordList = localStorage.keywordList ? JSON.parse(localStorage.keywordList) : []
  },
  methods: {
    async saveKeyword() {
      this.keywordList.push(this.searchKeyword)
      // 去重
      this.keywordList = Array.from(new Set(this.keywordList))
      // 大于5取最后5个
      if (this.keywordList.length > 5) {
        this.keywordList = this.keywordList.slice(this.keywordList.length - 5, this.keywordList.length)
      }

      localStorage.keywordList = JSON.stringify(this.keywordList)
      const { data: res } = await searchMusic(this.searchKeyword)
      console.log(res.result.songs)
      this.searchMusic = res.result.songs
      console.log(this.searchMusic)
    },
    play(item, i) {
      this.$store.commit('pushPlayList', item)
      this.$store.commit('setPlayCurrentIndex', this.$store.state.playList.length - 1)
      console.log(i)
      console.log(item)
    },
    historySearch(item) {
      this.searchKeyword = item
      this.saveKeyword()
    }
  }
}
</script>

<style lang="less" scoped>
#search-header {
  width: 7.5rem;
  padding: 0 0.4rem;
  .top {
    margin-top: 0.5rem;
    display: flex;
    width: 100%;
    height: 0.5rem;
    align-items: center;
    .icon {
      width: 0.45rem;
      height: 0.45rem;
      margin-right: 0.2rem;
    }
    input {
      flex: 1;
      margin-left: 0.1rem;
      border: none;
      outline: none;
      border-bottom: 1px solid #666;
    }
  }
  .history {
    margin-top: 0.5rem;
    display: flex;
    .historyLeft {
      font-size: 0.3rem;
      margin-right: 0.3rem;
      font-weight: bold;
    }
    .historyRight {
      display: flex;
      flex-wrap: wrap;
      width: 5rem;
      .kyewordItem {
        margin: 0 0.2rem 0.2rem 0;
        padding: 0 0.2rem;
        background-color: rgb(230, 230, 230);
        border-radius: 0.2rem;
      }
    }
  }

  .music-list {
    width: 100%;
    margin-bottom: 1rem;
    .list-item {
      height: 1.2rem;
      display: flex;
      align-items: center;
      .index {
        width: 0.6rem;
        text-align: center;
        font-size: 0.25rem;
        color: #ccc;
      }
      .music-name {
        font-size: 0.25rem;
        flex: 1;
      }
      .icon {
        width: 0.3rem;
        height: 0.3rem;
        margin-right: 0.3rem;
      }
    }
  }
}
</style>